// pages/personal/personal.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    wximg: '',
    userName: '',
    nickName: '',
    school: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    //获取用户信息
    wx.request({
      url: app.globalData.url + '/user/personal',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: app.globalData.userInfo.openid
      },
      success: res => {
        console.log(res);
        that.setData({
          wximg: res.data.wximg,
          userName: res.data.userName,
          nickName: res.data.nickName,
          school: res.data.school
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  modifyPersonal: function(){
    this.setData({
      showModal: true
    })
  },
  /**
   * 弹出框蒙层截断touchmove事件
   */
  preventTouchMove: function () { },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    var that = this;

    //修改用户信息
    wx.request({
      url: app.globalData.url + '/updateUser',
      method: 'PUT',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        openid: app.globalData.userInfo.openid,
        userName: that.data.userName,
        school: that.data.school
      },
      success: res => {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          duration: 3000
        });
        that.setData({
          showModal: false,
          userName: res.data.userName,
          school: res.data.school
        });
        app.globalData.userInfo.userName = res.data.userName;
      },
      fail: err => {
        wx.showToast({
          title: '失败',
          icon: 'error',
          duration: 3000
        });
      }
    });
  },

  inputNameChange: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  inputSchoolChange: function (e) {
    this.setData({
      school: e.detail.value
    })
  }
})